<?php

namespace app\modules\Contract\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\modules\Contract\models\Contract;

/**
 * ContractSearch represents the model behind the search form of `app\modules\Contract\models\Contract`.
 */
class ContractSearch extends Contract
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'type', 'customer_id', 'sale_id', 'oper_id', 'finance_id', 'is_invoice', 'is_back', 'user_id', 'status'], 'integer'],
            [['code', 'sale_currency', 'payment_type', 'pay_currency', 'exchange_currency', 'express_num', 'invoice_date', 'invoice_no', 'invoice_company', 'invoice_currency', 'back_date', 'back_company', 'back_currency', 'created_at', 'updated_at'], 'safe'],
            [['sale_price', 'pay_price', 'exchange_price', 'invoice_price', 'back_price'], 'number'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Contract::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'type' => $this->type,
            'customer_id' => $this->customer_id,
            'sale_id' => $this->sale_id,
            'sale_price' => $this->sale_price,
            'pay_price' => $this->pay_price,
            'exchange_price' => $this->exchange_price,
            'oper_id' => $this->oper_id,
            'finance_id' => $this->finance_id,
            'is_invoice' => $this->is_invoice,
            'invoice_date' => $this->invoice_date,
            'invoice_price' => $this->invoice_price,
            'is_back' => $this->is_back,
            'back_date' => $this->back_date,
            'back_price' => $this->back_price,
            'user_id' => $this->user_id,
            'status' => $this->status,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        $query->andFilterWhere(['like', 'code', $this->code])
            ->andFilterWhere(['like', 'sale_currency', $this->sale_currency])
            ->andFilterWhere(['like', 'payment_type', $this->payment_type])
            ->andFilterWhere(['like', 'pay_currency', $this->pay_currency])
            ->andFilterWhere(['like', 'exchange_currency', $this->exchange_currency])
            ->andFilterWhere(['like', 'express_num', $this->express_num])
            ->andFilterWhere(['like', 'invoice_no', $this->invoice_no])
            ->andFilterWhere(['like', 'invoice_company', $this->invoice_company])
            ->andFilterWhere(['like', 'invoice_currency', $this->invoice_currency])
            ->andFilterWhere(['like', 'back_company', $this->back_company])
            ->andFilterWhere(['like', 'back_currency', $this->back_currency]);

        return $dataProvider;
    }
}
